package com.gy.yemall.yemallproduct.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gy.yemall.yemallproduct.entity.CategoryEntity;
import com.gy.yemall.yemallproduct.service.CategoryService;
import com.gy.common.utils.PageUtils;
import com.gy.common.utils.R;



/**
 * 商品三级分类
 *
 * @author ye
 * @email 2900801226@qq.com
 * @date 2021-09-30 16:28:53
 */
@RestController
@RequestMapping("yemallproduct/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询所有种类以及该种类的子类，以树形结构展现出来
     */
    @RequestMapping("/list/tree")
    // @RequiresPermissions("yemallproduct:category:list")
    public R list(@RequestParam Map<String, Object> params){

        List<CategoryEntity> categoryEntityList = categoryService.listWithTree();

        return R.ok().put("data", categoryEntityList);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    // @RequiresPermissions("yemallproduct:category:info")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("yemallproduct:category:save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("yemallproduct:category:update")
    public R update(@RequestBody CategoryEntity category){
		// categoryService.updateById(category);
        categoryService.updateCategoryAndC_B_Relation(category);
        return R.ok();
    }

    /**
     * 删除
     * @RequestBody代表只能接受post请求中的请求体里面的内容
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("yemallproduct:category:delete")
    public R delete(@RequestBody Long[] catIds){
		// categoryService.removeByIds(Arrays.asList(catIds));
        categoryService.removeMenuByIds(Arrays.asList(catIds));
        return R.ok();
    }

}
